﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Sdepci.InfoCode.Web.Authority
{
    public partial class MenuIconSelector : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                this.ViewState["Template"] = this.Template.InnerHtml;
                this.BindData();
            }
        }

        private void BindData()
        {
            string current = "default.gif";
            if (this.Request.QueryString["FuncIcon"] != null && this.Request.QueryString["FuncIcon"].Trim().Length > 0)
            {
                current = this.Request.QueryString["FuncIcon"].Trim();
            }

            string html = string.Empty;
            string path = Server.MapPath("../Images/MenuIcon");
            DirectoryInfo dir = new DirectoryInfo(path);
            FileInfo[] files = dir.GetFiles();
            foreach (FileInfo file in files)
            {
                if (file.Extension == ".gif" || file.Extension == ".jpg" || file.Extension == ".png" || file.Extension == ".ico")
                {
                    string template = this.ViewState["Template"].ToString();

                    template = template.Replace("{MenuIcon}", file.Name);

                    if (file.Name == current)
                    {
                        template = template.Replace("{checked}", "checked=\"checked\"");
                    }
                    else
                    {
                        template = template.Replace("{checked}", "");
                    }

                    html += template;
                }
            }

            this.Template.InnerHtml = html;
        }
    }
}
